package edu.bbk.muc.lifetracker.apitest;

import android.location.Location;
import edu.bbk.muc.lifetracker.common.Acceleration;
import edu.bbk.muc.lifetracker.common.SensorsToUi;
import edu.bbk.muc.lifetracker.common.SimpleLocation;

/**
 * With 3 seconds timeout this mock should form exactly two events:
 * <ol>
 * <li>Location(60,0,200); AmbientLight(180)</li>
 * <li>Acceleration(1,2,3); Location(70,0,200); AmbientLight(200);</li>
 * </ol>
 */
public class SensorsMockThread implements Runnable {
  private SensorsToUi sensors;

  public SensorsMockThread(SensorsToUi sensor) {
    this.sensors = sensor;
  }

  @Override
  public void run() {
    try {
      long startTimeUsec = System.currentTimeMillis() * 1000000;
      sensors.onAmbientLightChanged(
          startTimeUsec,
          160);
      startTimeUsec += 1e6;
      Thread.sleep(1000);

      sensors.onLocationChanged(
          startTimeUsec,
          makeLocation(50, 0, 100));
      sensors.onAmbientLightChanged(
          startTimeUsec,
          170);
      startTimeUsec += 5e5;

      Thread.sleep(500);

      sensors.onAmbientLightChanged(
          startTimeUsec,
          180);
      startTimeUsec += 1e6;

      Thread.sleep(1000);

      sensors.onLocationChanged(
          startTimeUsec,
          makeLocation(60, 0, 200));
      startTimeUsec += 5e5;

      Thread.sleep(500);

      // 3 seconds, should be timeout
      sensors.onAccelerationChanged(
          startTimeUsec,
          new Acceleration(1f, 2f, 3f));
      startTimeUsec += 1e6;

      Thread.sleep(1000);

      sensors.onLocationChanged(
          startTimeUsec,
          makeLocation(70, 0, 200));
      startTimeUsec += 200;

      sensors.onAmbientLightChanged(
          startTimeUsec,
          200);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }

  private SimpleLocation makeLocation(float lat, float lon, float alt) {
    Location location = new Location("fake");
    location.setLatitude(lat);
    location.setLongitude(lon);
    location.setAltitude(alt);
    return new SimpleLocation(location);
  }
}
